package org.dawn.deveco.plugin.resourceeditor.utils;

import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import org.dawn.deveco.plugin.resourceeditor.common.Constants;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Base64;
import java.util.function.Function;

public class SwingUtils {
    private static final Logger LOG = Logger.getInstance(SwingUtils.class);

    public static ImageIcon shrinkIconFromBase64(String iconBase64, int iconHeight) {
        ImageIcon icon = new ImageIcon(Base64.getDecoder().decode(iconBase64));
        Image img = icon.getImage().getScaledInstance(iconHeight, iconHeight, Image.SCALE_SMOOTH);
        return new ImageIcon(img);
    }

    public static ImageIcon shrinkIcon(String iconPath, int iconHeight) {
        ImageIcon icon = null;
        if (iconPath == null) {
            return null;
        }
        if (!Files.exists(Paths.get(iconPath))) {
            try (InputStream inputStream = SwingUtils.class.getResourceAsStream(iconPath)) {
                icon = new ImageIcon(inputStream.readAllBytes());
            } catch (IOException e) {
                throw new IllegalArgumentException("Fail to load resource: " + iconPath);
            }
        } else {
            icon = new ImageIcon(iconPath);
        }
        Image img = icon.getImage().getScaledInstance(iconHeight, iconHeight, Image.SCALE_SMOOTH);
        return new ImageIcon(img);
    }

    public static String chooseFolder(Project project, String title, String description) {
        return chooseFolder(project, title, description, false);
    }

    public static String chooseFolder(Project project, String title, String description, boolean showResultDialog) {
        FileChooserDescriptor chooserDescriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
        chooserDescriptor.setTitle(title);
        chooserDescriptor.setDescription(description);
//        if (project != null) {
//            chooserDescriptor.setRoots(project.getBaseDir());
//        }
        chooserDescriptor.setShowFileSystemRoots(true);
        VirtualFile virtualFile = FileChooser.chooseFile(chooserDescriptor, project, null);
        if (virtualFile != null) {
            return virtualFile.getPath();
        } else {
            if (showResultDialog) {
                Messages.showMessageDialog("未选择文件夹", "提示", Messages.getInformationIcon());
            }
        }
        return null;
    }

    public static String chooseFile(Project project, String title, String description, Function<String, Boolean> filter) {
        return chooseFile(project, title, description, false, filter);
    }
    public static String chooseFile(Project project, String title, String description, boolean showResultDialog, Function<String, Boolean> filter) {
        FileChooserDescriptor chooserDescriptor = FileChooserDescriptorFactory.createSingleLocalFileDescriptor()
                .withFileFilter(fileName -> filter.apply(fileName.getCanonicalPath()));
        chooserDescriptor.setTitle(title);
        chooserDescriptor.setDescription(description);
        if (project != null) {
            chooserDescriptor.setRoots(project.getBaseDir());
        }
        chooserDescriptor.setShowFileSystemRoots(true);
        VirtualFile virtualFile = FileChooser.chooseFile(chooserDescriptor, project, null);
        if (virtualFile != null) {
            return virtualFile.getPath();
        } else {
            if (showResultDialog) {
                Messages.showMessageDialog("未选择文件", "提示", Messages.getInformationIcon());
            }
        }
        return null;
    }

    public static String chooseImageFile(Project project, String title, String description) {
        return chooseImageFile(project, title, description, false);
    }
    public static String chooseImageFile(Project project, String title, String description, boolean showResultDialog) {
        return chooseFile(project, title, description, showResultDialog, filePath -> {
            File file = new File(filePath);
            String fileExt = file.getName().substring(file.getName().lastIndexOf('.') + 1);
            return Arrays.stream(Constants.IMAGE_FILE_EXTENSIONS).anyMatch(ext -> ext.equalsIgnoreCase(fileExt/*file.getExtension()*/));
        });
    }
}
